package org.hank.compose_yyzs.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.tencent.mmkv.MMKV
import org.hank.compose_yyzs.constants.HttpConfig
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
import org.hank.compose_yyzs.R
import org.hank.compose_yyzs.bean.request.AppUserChangePwdDto
import org.hank.compose_yyzs.constants.Constants
import org.hank.compose_yyzs.http.ApiService
import org.hank.compose_yyzs.utils.NetWorkUtils
import org.hank.compose_yyzs.utils.RsaUtils
import org.hank.compose_yyzs.utils.getStringGlobal
import javax.inject.Inject

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 *@auth: Hank
 *邮箱: cs16xiaoc1@163.com
 *创建时间: 2023/9/13 14:43
 *描述:
 */
@HiltViewModel
class UserInfoViewModel @Inject constructor(var apiService: ApiService) : ViewModel() {

    private val _viewEvents = Channel<UserInfoViewEvent>(Channel.BUFFERED)
    val viewEvents = _viewEvents.receiveAsFlow()
    var userName = MutableStateFlow("")
    var userPhone = MutableStateFlow("")
    var userEmail = MutableStateFlow("")

    var passWordOld = MutableStateFlow("")
    var passOldFocus = MutableStateFlow(false)
    var passWordNew = MutableStateFlow("")
    var passNewFocus = MutableStateFlow(false)
    var passWordNewConfirm = MutableStateFlow("")
    var passConfirmFocus = MutableStateFlow(false)

    fun getUserInfo() {
        viewModelScope.launch {
            var currentId = MMKV.defaultMMKV().getInt(Constants.USER_ID, -1)
            flow { emit(apiService.getUserInfo(currentId)) }
                .catch {
                    _viewEvents.send(
                        UserInfoViewEvent.ErrMessage(NetWorkUtils.generateMessageByException(it))
                    )
                }.collect {
                    userName.emit(it.data.name ?: "")
                    userPhone.emit(it.data.phone ?: "")
                    userEmail.emit(it.data.email ?: "")
                }
        }
    }

    fun updatePassWord() {
        viewModelScope.launch {
            if (passWordOld.value.isEmpty()) {
                _viewEvents.send(UserInfoViewEvent.ErrMessage(getStringGlobal(R.string.userinfo_old_pass_empty)))
                return@launch
            }

            if (passWordNew.value.isEmpty()) {
                _viewEvents.send(UserInfoViewEvent.ErrMessage(getStringGlobal(R.string.userinfo_new_pass_empty)))
                return@launch
            }
            if (passWordNewConfirm.value != passWordNew.value) {
                _viewEvents.send(UserInfoViewEvent.ErrMessage(getStringGlobal(R.string.userinfo_new_pass_err)))
                return@launch
            }

            flow {
                emit(
                    apiService.changePassword(
                        AppUserChangePwdDto(
                            newpassword1 =
                            passWordNewConfirm.value,
                            password =
                            passWordOld.value
                        )
                    )
                )
            }
                .catch {
                    _viewEvents.send(
                        UserInfoViewEvent.ErrMessage(NetWorkUtils.generateMessageByException(it))
                    )
                }.collect {
                    if (it.httpIsSuccess)
                        _viewEvents.send(UserInfoViewEvent.UpdatePassSuccess)
                    else
                        _viewEvents.send(UserInfoViewEvent.ErrMessage(it.message ?: ""))
                }
        }
    }
}

sealed class UserInfoViewEvent {
    data class ErrMessage(var errMessage: String) : UserInfoViewEvent()

    object UpdatePassSuccess : UserInfoViewEvent()
}